//
//  DXpopButtonOne.m
//  UILbrary
//
//  Created by mac on 16/7/20.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DXpopButtonOne.h"
#import "Constant.h"
#import "BaseButton.h"

static CGFloat const buttonSize = 80;
static NSTimeInterval const kAnimalPopAndDismmisTime = 0.8;
static CGFloat const bgHeigh = 300;
@interface DXpopButtonOne ()

@property (nonatomic, assign) BOOL isTopDismiss;
@end
@implementation DXpopButtonOne

- (instancetype)initWithFrame:(CGRect)frame{
    if (self             = [super initWithFrame:kApp_Screen_bounds]) {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    self.isTopDismiss    = YES;
    }
    return self;
}


- (void)popBottom{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    self.isTopDismiss = NO;
    [self.bottomArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CGFloat buttonOrignX         = (kAPP_With - 3*buttonSize)/4.0 + (buttonSize + (kAPP_With - 3*buttonSize)/4.0) * (idx % 3);

        BaseButton *button           = [[BaseButton alloc] initWithFrame:CGRectMake(buttonOrignX, kAPP_Hight, buttonSize,  buttonSize)];
        [button setImage:[UIImage imageNamed:self.bottomArr[idx]] forState:UIControlStateNormal];
        [button setTitle:self.bottomTitleArr[idx] forState:UIControlStateNormal];
        button.isTopImageBottomTitle = YES;
        
        [button bk_addEventHandler:^(id sender) {
            
            self.bottomButtonClick(idx,button.titleLabel.text);
        } forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        BOOL isDlay                  = idx>=3 ? YES : NO;
        
        [UIView animateWithDuration:0.5 delay:isDlay?0.1:0 usingSpringWithDamping:10 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            CGFloat buttonOrignX = (kAPP_With - 3*buttonSize)/4.0 + (buttonSize + (kAPP_With - 3*buttonSize)/4.0) * (idx % 3);
            CGFloat buttonOrinY  = kAPP_Hight - bgHeigh + (bgHeigh - 2*buttonSize)/3.0 + ((bgHeigh - 2*buttonSize)/3.0 + buttonSize) * (idx/3);
            
            UINavigationBar *bar = [UINavigationBar appearance];
            
            if(!bar.isTranslucent){
            buttonOrinY          = buttonOrinY - 64;
            }
            
            button.frame         = CGRectMake(buttonOrignX, buttonOrinY, buttonSize, buttonSize);
            
        } completion:^(BOOL finished) {
 
        }];
    }];
}

- (void)popTop{
    [self clean];
    CGFloat raduis = buttonSize ;
    self.isTopDismiss = YES;
    CGPoint circlePoin = CGPointMake(self.center.x, kAPP_Hight - buttonSize);
    [self.topImageArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        BaseButton *button = [[BaseButton alloc] initWithFrame:CGRectMake(kAPP_With/2.0 - buttonSize/2, kAPP_Hight, buttonSize, buttonSize)];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",obj]] forState:UIControlStateNormal];
        [button setTitle:self.topTitleArr[idx] forState:UIControlStateNormal];
        button.isTopImageBottomTitle = YES;
        [self addSubview:button];
        
        [button bk_addEventHandler:^(id sender) {
            if (idx == 1) {
                [self popBottom];
            }
            self.topButtonClick(idx, button.titleLabel.text);
        } forControlEvents:UIControlEventTouchUpInside];
        
        button.transform = CGAffineTransformMakeRotation(M_PI_2/2);
        //虽然在循环里，动画会在循环结束后，一起出现
        [UIView animateWithDuration:kAnimalPopAndDismmisTime delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            button.alpha = 1;
            button.transform = CGAffineTransformIdentity;
            button.center = CGPointMake(circlePoin.x + raduis * cos(M_PI/2.0*idx), circlePoin.y - (buttonSize + raduis * sin(M_PI/2.0*idx)));
        } completion:^(BOOL finished) {
            
        }];
    }];
}


- (void)clean
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissIsTop:self.isTopDismiss];
}
- (void)dismissIsTop:(BOOL)isTop{
    //两种不同的dismiss效果
    if (isTop) {
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
                
                obj.center = CGPointMake(kAPP_With/2.0 - buttonSize/2.0, kAPP_Hight);
                obj.transform = CGAffineTransformMakeScale(.20, .20);
            } completion:^(BOOL finished) {
                [obj removeFromSuperview];
                if (idx == self.subviews.count) {
                    [self removeFromSuperview];
                }
            }];
            
        }];

    }else{
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [UIView animateWithDuration:0.25 animations:^{
                CGFloat buttonOrignX = (kAPP_With - 3*buttonSize)/4.0 + (buttonSize + (kAPP_With - 3*buttonSize)/4.0) * (idx % 3);
                obj.frame = CGRectMake(buttonOrignX, kAPP_Hight, buttonSize,  buttonSize);
            } completion:^(BOOL finished) {
                [obj removeFromSuperview];
                if (idx == self.subviews.count) {
                    [self removeFromSuperview];
                }
            }];
        }];
    }
    
    
    
}


#pragma mark - 防止属性数组内的元素超出所需的范围   top 只需要3个元素   bottom 只需要6个元素
- (void)setTopImageArr:(NSArray *)topImageArr{
    if (topImageArr.count > 3) {
        _topImageArr = [topImageArr subarrayWithRange:NSMakeRange(0, 3)];
    }else{
        _topImageArr = topImageArr;
    }
}

- (void)setTopTitleArr:(NSArray *)topTitleArr{
    if (topTitleArr.count >3) {
        _topTitleArr = [topTitleArr subarrayWithRange:NSMakeRange(0, 3)];
    }else{
        _topTitleArr = topTitleArr;
    }
}

- (void)setBottomArr:(NSArray *)bottomArr{
    if (bottomArr.count >6) {
        _bottomArr = [bottomArr subarrayWithRange:NSMakeRange(0, 6)];
    }else{
        _bottomArr = bottomArr;
    }
}

- (void)setBottomTitleArr:(NSArray *)bottomTitleArr{
    if (bottomTitleArr.count > 6) {
        _bottomTitleArr = [bottomTitleArr subarrayWithRange:NSMakeRange(0, 6)];
    }else{
        _bottomTitleArr = bottomTitleArr;
    }
}



@end
